import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/register',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "about" */ '../views/Register.vue')
  },
  {
    path: '/login',
    component: () => import('../views/Login')
  },
  {
    path: '/userInfo',
    component: () => import('../views/UserInfo'),
    meta: {
      istoken: true
    }
  },
  {
    path: '/edit',
    component: () => import('../views/Edit')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  if (!localStorage.getItem('token') && !localStorage.getItem('id') && to.meta.istoken === true) {
    this.router.push('/login')
    this.$msg.fail('请重新登录')
    return
  }
  next()
})

export default router
